<template>
  <a-modal :title="modal.title" :visible="modal.visible" maskClosable width="900px" @cancel="$emit('done')"
    @ok="submit">
    <a-form :form="form" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
      <a-form-item label="id" style="display: none;">
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item label="productId" style="display: none;">
        <a-input v-decorator="['productId']" />
      </a-form-item>
      <a-form-item label="productiIndemnifyId" style="display: none;">
        <a-input v-decorator="['productiIndemnifyId']" />
      </a-form-item>

      <a-form-item label="费率-类型">
        <a-radio-group v-decorator="['rateType', { rules: [{ required: true, message: '请选择【费率-类型】' }] }]">
          <a-radio :value="0">必选责任</a-radio>
          <a-radio :value="1">可选责任</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="是否区分男女">
        <a-radio-group v-decorator="['sexyType', { rules: [{ required: true, message: '请选择【是否区分男女】' }] }]">
          <a-radio :value="0">否</a-radio>
          <a-radio :value="1">是</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="年龄">
        <a-input-number :min="0" :precision="0"
          v-decorator="['age', { rules: [{ required: true, message: '请输入【年龄】' }] }]"
          :style="{ width: '100%', textAlign: 'center', display: 'inline-block' }" />
      </a-form-item>
      <a-form-item label="男费率金额">
        <a-input-number :min="0" :precision="0"
          v-decorator="['feeMale', { rules: [{ required: true, message: '请输入【男费率金额】' }] }]"
          :style="{ width: '100%', textAlign: 'center', display: 'inline-block' }" />
      </a-form-item>
      <a-form-item label="女费率金额">
        <a-input-number :min="0" :precision="0"
          v-decorator="['feeFemale', { rules: [{ required: true, message: '请输入【女费率金额】' }] }]"
          :style="{ width: '100%', textAlign: 'center', display: 'inline-block' }" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
  import { add, update } from '@/api/aibao/product/payRate'

  import pick from 'lodash.pick'
  const fields = ['id', 'productId', 'productiIndemnifyId', 'rateType', 'age', 'sexyType', 'feeMale', 'feeFemale']

  export default {
    props: {
      modal: {
        type: Object,
        required: true
      },
    },
    data() {
      return {
        form: this.$form.createForm(this)
      }
    },
    created() {
      fields.forEach(v => this.form.getFieldDecorator(v))
      this.$watch('modal', () => {
        if (this.modal) {
          this.form.setFieldsValue(pick(this.modal.form, fields))
        }
      }, { deep: true })
    },
    methods: {
      submit() {
        this.form.validateFields((errors, values) => {
          if (!errors) {
            const api = values.id ? update : add
            api(values)
              .then(res => {
                this.$message.success('操作成功')
                this.$emit('done')
              })
              .catch(() => {})
          }
        })
      },
    },
  }
</script>